{#
@author David Alexa <alexa.david@me.com>

Copyright (C) 2012-2015 CESNET

LICENSE TERMS

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in
  the documentation and/or other materials provided with the
  distribution.
3. Neither the name of the Company nor the names of its contributors
  may be used to endorse or promote products derived from this
  software without specific prior written permission.

ALTERNATIVELY, provided that this notice is retained in full, this
product may be distributed under the terms of the GNU General Public
License (GPL) version 2 or later, in which case the provisions
of the GPL apply INSTEAD OF those given above.

This software is provided ``as is'', and any express or implied
warranties, including, but not limited to, the implied warranties of
merchantability and fitness for a particular purpose are disclaimed.
In no event shall the company or contributors be liable for any
direct, indirect, incidental, special, exemplary, or consequential
damages (including, but not limited to, procurement of substitute
goods or services; loss of use, data, or profits; or business
interruption) however caused and on any theory of liability, whether
in contract, strict liability, or tort (including negligence or
otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.
#}

{% set isSortable = false %}
{% if element.attributes.orderedBy is defined
	and element.attributes.orderedBy == "user"
	and (element.attributes.eltype == "leaf-list" or element.attributes.eltype == "list") %}
	{% set isSortable = true %}
{% endif %}

<div class="leaf-line
	{% if addLevel is defined and addLevel == true %} level-{{level}}{% endif %}
	{% if hasChildren is defined and hasChildren == true %} has-children{% endif %}
	{% if element.attributes.iskey is defined and element.attributes.iskey == "true" %} is-key{% endif %}
	{% if element.attributes.eltype is defined %}
		type-{{element.attributes.eltype}}
		{% if isSortable %}
		sortable-node
		{% endif %}
	{% else %}
		type-undefined
	{% endif %}
	{% if isRootElem is defined and isRootElem == true %}is-root-elem{% endif %}

	config
">
	<span class="label">
		<span class="dots"></span>
		{% if element.attributes.description is defined %}
			<span class="tooltip-cover help">
				<span class="icon-help">?</span>
				<span class="tooltip-description">{{element.attributes.description}}</span>
			</span>
		{% endif %}
		<span class="label-cover {% if element.attributes.description is defined %}has-tooltip{% endif %}">
			{% if hasChildren is defined and hasChildren == true %}
				<span class="bull"></span>
			{% else %}
				<span class="dash"></span>
			{% endif %}
			<strong>{{key}}</strong>
			{% if element.attributes.mandatory is defined %}
				<span class="mandatory tooltip-cover">
				<span class="icon-help">*</span>
				<span class="tooltip-description">Mandatory item</span>
				</span>
			{% endif %}
		</span>
	</span>

	{% block editBar %}
	{% set editBarShouldBeVisible =
	(element.attributes.eltype is defined
		and (
			element.attributes.eltype == "list"
			or element.attributes.eltype == "leaf-list"
			or element.attributes.eltype == "container"
		)
		and (
			(hideEditBar is defined and hideEditBar != true)
			or hideEditBar is not defined
		)
	) or (
		forceShowEditBar is defined and forceShowEditBar == true
	)
	%}

	{% set forceHideEditBar = forceShowEditBar is defined and forceShowEditBar == false %}

	{% if
	(element.attributes.mandatory is not defined
		or editBarShouldBeVisible
	) and forceHideEditBar != true
	%}
		<span class="edit-bar">
			{% if isSortable is defined and isSortable %}
			<img src="{{ asset('bundles/fitmoduledefault/images/ico/sort_item.png')}}" alt="Sort" title="Sort" class="sort-item" />
			{% endif %}
			{% if editBarShouldBeVisible %}
				<img src="{{ asset('bundles/fitmoduledefault/images/ico/add_child.png')}}" alt="Add child" title="Add child" class="create-child" rel="{{ xpath|replace({'/': '--', '[': '?', ']': '!'}) }}{#_{{ path("generateFromModel", {"level": level, "xPath": xpath|replace({'/': '-', '[': '?', ']': '!'})|url_encode, "key": routeParams.key, "module": routeParams.module, "subsection": routeParams.subsection} ) }}#}" data-typeahead-path="{{ valuesTypeaheadPath }}"/>
				{#<img src="{{ asset('bundles/fitmoduledefault/images/ico/add_sibling.png')}}" alt="Duplicate node" title="Duplicate node" rel="{{ xpath|replace({'/': '--', '[': '?', ']': '!'}) }}" class="sibling" />#}
			{% endif %}
			<img src="{{ asset('bundles/fitmoduledefault/images/ico/remove_child.png')}}" alt="Remove child" title="Remove child" class="remove-child" rel="{{ xpath|replace({'/': '--', '[': '?', ']': '!'}) }}" />
		</span>
	{% endif %}
	{% endblock editBar %}

	{% block configInputElem %}
	{% set currentInputName = "configDataForm[" ~ key ~ "_" ~ xpath|replace({'/': '--', '[': '?', ']': '!'}) ~ "]" %}
	{% if element.0 is defined and element.0|length == 0 %}
			{# we must set this variable again, because we render only block configInputElem #}
			{% set currentInputName = "configDataForm[" ~ key ~ "_" ~ xpath|replace({'/': '--', '[': '?', ']': '!'}) ~ "]" %}
			{% if element.attributes.type is defined and element.attributes.type == 'boolean' %}
				<span class="config-value-cover radio-cover">
					<label>
						<input
								type="radio"
								value="true"
								name="{{currentInputName}}"
								{% if element.0 == "true" %}checked="checked"{% endif %}
								{% if element.attributes.default is defined %}default="{{element.attributes.default}}"{% endif %}
								/>
						true
					</label>
					<label>
						<input
								type="radio"
								value="false"
								name="{{currentInputName}}"
								{% if element.0 == "false" %}checked="checked"{% endif %}
								{% if element.attributes.default is defined %}default="{{element.attributes.default}}"{% endif %}
								/>
						false
					</label>
				</span>
			{% elseif element.attributes.type is defined and (element.attributes.type == 'enumeration' or element.attributes.type == 'identityref') %}
				<span class="config-value-cover">
					<select name="{{currentInputName}}">
						{% if element.attributes.type == 'identityref'
							and element.attributes.enumval == ''
							and (moduleIdentityRefs is defined and element.attributes.identitybasename is defined) %}
							{% set identityKey = ''~element.attributes.identitybasename~'' %}
							{% set values = (moduleIdentityRefs[identityKey] is defined) ? moduleIdentityRefs[identityKey] : [] %}
						{% else %}
							{% set values = explode('|', element.attributes.enumval) %}
						{% endif %}
						{% for val in values %}
							<option value="{{val}}" {% if element.0 == val %}selected="selected"{% endif %}>{{val}}</option>
						{% endfor %}
					</select>
				</span>
			{% else %}
				{% set inputType = "text" %}
				{% set inputClass = "text" %}
				{% set min = "" %}
				{% set max = "" %}

				{% if element.attributes.type is defined %}
					{% if isNumberType(element.attributes.type) %}
						{% set inputType = "number" %}
						{% if element.attributes.range is defined %}
							{% set minMax = explode("..", element.attributes.range) %}
							{% if minMax|length == 2 %}
								{% set min = 'min=' ~ minMax[0] %}
								{% set max = 'max=' ~ minMax[1] %}
								{% set inputType = "range" %}
								{% set inputClass = "range" %}
							{% endif %}
						{% endif %}
					{% elseif isUrlType(element.attributes.type) %}
						{% set inputType = "url" %}
					{% else %}
						{# todo try to find "typedef" definition in model and substitute it #}
						{% set inputType = "text" %}
					{% endif %}
				{% endif %}
				{% if element.attributes.iskey is defined and element.attributes.iskey == "true" %}
					{% set disabledInput = 'disabled=true' %}
				{% else %}
					{% set disabledInput = "" %}
				{% endif %}

				<input
						type="{{inputType}}"
						class="{{inputClass}} value"
						value="{{element.0}}"
						name="{{currentInputName}}"
						{{disabledInput}}
						{{min}} {{max}}
						{% if element.attributes.default is defined %}default="{{element.attributes.default}}" title="Default value: {{element.attributes.default}}"{% endif %}
						/>
			{% endif %}
	{% else %}
		{% if
		(useHiddenInput is defined and useHiddenInput == true) or
		(element.attributes.eltype is defined and
			(element.attributes.eltype == "leaf-list" or element.attributes.eltype == "list")
		) %}
			<input type="hidden" value="" name="{{ currentInputName }}" class="hidden-input-value">
		{% endif %}
		{% if (useHiddenInput is not defined or useHiddenInput != true) %}
			<span class="value"></span>
		{% endif %}
	{% endif %}
	{% endblock configInputElem %}

	{% if 0 and element.attributes.type is defined %}
	{% endif %}
</div>